//Make all this an extension of the select object
bla = {
    _this = this;
    
    function parseOption(option){
        var result = "";
        if((typeof option) == 'undefined'){
            //throw error;
            console.log('Error');
        }else if((typeof option) == 'string'){
            var pat = /<option[\s\S*]*>\s*\S+\s*<\/option>/i;
            if(pat.test(option)){
                result = option;
            }else{
                result = '<option value="'+option+'">'+option+'</option>';
            }
            console.log('string ' + result);
        }else if((typeof option) == 'object'){
            result = '<option';
            //lets complete the option attributes
            for (k in option){
                if(k != 'inner'){
                    result += ' ' + k + '="' + option[k] + '"';
                }
            }
            result += '>';
            if(option['inner'] != undefined){
                result += option['inner'];
            }
            result += '</option>';
            console.log('object ' + result);
        }
        
        return result;
    }
    
    this.length = function(){
        //TODO:verify browser compatibility
        return select.length;
    }
    
    this.removeOption = function(index){
        var children = $(select).children('option');
        if(isNaN(index)) index = children.length -1;
        if(index < 0) index = children.length + index; //We are going to allow index below 0 like Python :D
        //index validation
        if(index < 0 || index >= children.length){
            throw "index out of bounds";
        }
        $(children[index]).remove();
    }
    
    this.removeAllOptions = function(){
        var children = $(select).children('option').remove();
    }
    
    this.addOption = function(option, index){
        var children = $(select).children('option');
        if(isNaN(index)) index = children.length;
        if(index < 0) index = children.length + 1 + index; //We are going to allow index below 0 like Python :D
        //index validation
        if(index < 0 || index > children.length){
            throw "index out of bounds";
        }
        //if we've received an array of options, lets call us again for each option
        if(option.constructor == Array){
            console.log('array');
            for(i in option){
                this.addOption(option[i], index);
                index++;
            }
        }else{ //if the index is greater than 0 it means that the user want to insert the option between option or at the bottom
            if(index > 0){ 
                $(children[index-1]).after(parseOption(option));
            }else{//we are going to put options at the begining
                $(select).prepend(parseOption(option));
            }
        }
    };
    
    this.replaceOption = function(option, index){
        this.removeOption(index);
        this.addOption(option, index);
    };
    
    this.replaceAllOptions = function(option){
        this.removeAllOptions();
        this.addOption(option, 0);
    };
};    
    
$(function(){
    $('select.mrselect').each(function(){
        var id = $(this).attr('id');
        var a = new MRSelect(this);
        if(id){
            mrObjs[id] = a;
        }
    });
});

